/*********************************************************************************
 *      Copyright:  (C) 2021 Simply myself<1239726156@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  my_ini_parser.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2021年08月08日)
 *         Author:  Simply myself <1239726156@qq.com>
 *      ChangeLog:  1, Release initial version on "2021年08月08日 17时36分23秒"
 *                 
 ********************************************************************************/

#include "my_ini_parser.h"


int ini_parser(char *filename, struct mqtt_s *mqtt)
{
	int                  rv = 0;
	dictionary          *ini;
	char                 key[20];
	const char          *c_value = NULL;
	
	if ( !filename || !mqtt )
	{
		printf ("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return -1;
	}
	
	ini = iniparser_load(filename);
	if (ini == NULL) 
    {
        printf ("cannot parse file: %s\n", filename);
        return -2;
    }
#if 1
	
	if ( parser_str(ini, "mqtt:host", mqtt->mosq_ctx.host) < 0 )
	{
		rv = -3;	goto cleanup;
	}
	
	if ( parser_int(ini, "mqtt:port", &mqtt->mosq_ctx.port) < 0 )
	{
		rv = -4;	goto cleanup;
	}
	
	if ( parser_int(ini, "mqtt:keepalive", &mqtt->mosq_ctx.keepalive) < 0 )
	{
		rv = -5;	goto cleanup;
	}
	
	if ( parser_str(ini, "mqtt:clientid", mqtt->mosq_ctx.clientid) < 0 )
	{
		rv = -6;	goto cleanup;
	}
	
	if ( parser_str(ini, "mqtt:sub_topic", mqtt->mosq_ctx.sub_topic) < 0 )
	{
		rv = -7;	goto cleanup;
	}
	
	if ( parser_int(ini, "mqtt:sub_qos", &mqtt->mosq_ctx.sub_qos) < 0 )
	{
		rv = -8;	goto cleanup;
	}
	
	if ( parser_str(ini, "mqtt:pub_topic", mqtt->mosq_ctx.pub_topic) < 0 )
	{
		rv = -10;	goto cleanup;
	}
	
	if ( parser_int(ini, "mqtt:pub_qos", &mqtt->mosq_ctx.pub_qos) < 0 )
	{
		rv = -11;	goto cleanup;
	}
	
	if ( parser_str(ini, "usr:name", mqtt->usr.name) < 0 )
	{
		rv = -12;	goto cleanup;
	}
	
	if ( parser_str(ini, "usr:passwd", mqtt->usr.passwd) < 0 )
	{
		rv = -13;	goto cleanup;
	}
	
	if ( parser_str(ini, "payload_format:method", mqtt->payload_format.method) < 0 )
	{
		rv = -15;	goto cleanup;
	}
	
	if ( parser_str(ini, "payload_format:id", mqtt->payload_format.id) < 0 )
	{
		rv = -16;	goto cleanup;
	}	
	
	if ( parser_str(ini, "payload_format:version", mqtt->payload_format.version) < 0 )
	{
		rv = -17;	goto cleanup;
	}
	
	if ( parser_str(ini, "identifier:temp", mqtt->identifier.temp) < 0 )
	{
		rv = -18;	goto cleanup;
	}
	
	if ( parser_str(ini, "identifier:led", mqtt->identifier.led) < 0 )
	{
		rv = -19;	goto cleanup;
	}
	
	if ( parser_str(ini, "datebase:db_name", mqtt->DB.db_name) < 0 )
	{
		rv = -20;	goto cleanup;
	}
	
	if ( parser_str(ini, "datebase:table_name", mqtt->DB.table_name) < 0 )
	{
		rv = -21;	goto cleanup;
	}
	
	if ( parser_int(ini, "datebase:max_size", &mqtt->DB.max_size) < 0 )
	{
		rv = -22;	goto cleanup;
	}
	
	if ( parser_str(ini, "zlog:z_name", mqtt->zlog.z_name) < 0 )
	{
		rv = -23;	goto cleanup;
	}
	
#endif
	
#if 0
	/* 1 检查"mqtt:host"这个key在不在ini里 */
	strncpy(key, "mqtt:host", strlen("mqtt:host"));
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		rv = -2;	goto cleanup;
	}
	c_value = iniparser_getstring(ini, key, NULL);
	strncpy(mqtt->host, c_value, strlen(c_value));

	/*2*/
	memset(key, 0, sizeof(key));
	strncpy(key, "mqtt:port", strlen("mqtt:port"));
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		rv = -3;	goto cleanup;
	}
	mqtt->port = iniparser_getint(ini, key, -1);
		
	/*3*/
	memset(key, 0, sizeof(key));
	strncpy(key, "mqtt:keepalive", strlen("mqtt:keepalive"));
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		rv = -3;	goto cleanup;
	}
	mqtt->keepalive = iniparser_getint(ini, key, -1);

	/*4*/
	memset(key, 0, sizeof(key));
	strncpy(key, "mqtt:host", strlen("mqtt:host"));
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		rv = -2;	goto cleanup;
	}
	c_value = iniparser_getstring(ini, key, NULL);
	strncpy(mqtt->host, c_value, strlen(c_value));
	

#endif
	
	
cleanup:
	
    iniparser_freedict(ini);

	return rv;
}

/* 获取ini对象里的key键的字符串类型的值，赋给dist */
int parser_str(dictionary *ini, char *key, char *dist)
{
	const char 	*c_value = NULL;
	
	if ( !ini || !key || !dist )
	{
		printf ("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return -1;
	}
	
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		return -2;
	}
	c_value = iniparser_getstring(ini, key, NULL);
	strncpy(dist, c_value, strlen(c_value));

	return 0;
}

/* 获取ini对象里的key键的int类型的值，赋给dist */
int parser_int(dictionary *ini, char *key, int *dist)
{
	if ( !ini || !key || !dist )
	{
		printf ("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return -1;
	}
	
	if (iniparser_find_entry(ini, key) == 0)
	{
		printf ("The key[%s] is not fonud\n", key);
		return -2;
	}
	*dist = iniparser_getint(ini, key, -1);

	return 0;
}



